-
Couldn't load subscription status.
- Fork 2
BaseId: warn() instead of raise for duplicate prefixes #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the liberty of editing the PR description to give more context.
src/frequenz/core/id.py
Outdated
| raise TypeError("BaseId cannot be instantiated directly. Use a subclass.") | ||
| # We want to raise an exception here, but currently can't due to | ||
| # https://github.com/frequenz-floss/frequenz-repo-config-python/issues/421 | ||
| warn( | ||
| "BaseId cannot be instantiated directly. Use a subclass.", stacklevel=2 | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh lol. and I guess we have no test for this one 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we don't 🤷
Not sure if it is worth adding now (pytest have some utils to test warnings are emitted).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also add a test that BaseId raises if instantiated directly, this one should be easy too with with pytest.raises(TypeError, match="..."): BaseId(1))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
way ahead of you
Signed-off-by: Mathias L. Baumann <[email protected]>
Signed-off-by: Mathias L. Baumann <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR modifies the behavior of duplicate prefix handling in BaseId to issue a warning rather than immediately raising an exception. Key changes include:
- Adding tests for direct BaseId instantiation and non-unique prefix warning.
- Updating BaseId to use warn() for duplicate prefix detection.
- Adjusting release notes to reflect the change in behavior.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/test_id.py | Added tests to validate subclass instantiation and warning for duplicate prefixes. |
| src/frequenz/core/id.py | Changed duplicate prefix handling from raising ValueError to using warn(). |
| RELEASE_NOTES.md | Updated the release notes to summarize the change in duplicate prefix behavior. |
Comments suppressed due to low confidence (1)
src/frequenz/core/id.py:108
- The docstring for init_subclass still indicates that a ValueError is raised for duplicate prefixes, but the code now issues a warning. Consider updating the docstring to reflect the current behavior.
Raises:
| warn( | ||
| f"Prefix '{str_prefix}' is already registered. " | ||
| "ID prefixes must be unique." | ||
| "ID prefixes must be unique.", |
Copilot
AI
Jun 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is recommended to explicitly specify the warning category (e.g., UserWarning) in the warn() call for clarity and consistency, even though the default is UserWarning.
| "ID prefixes must be unique.", | |
| "ID prefixes must be unique.", | |
| category=UserWarning, |

This is only necessary because to cope with frequenz-floss/frequenz-repo-config-python#421. The check can raise an exception again once that it fixed.